//
//  SXTTabBarViewController.h
//
//
//  Created by ma c on 16/8/18.
//
//


#import "SXTTimeViewController.h"
#import "SDCycleScrollView.h"
@interface SXTTimeViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>

@property (nonatomic,strong) UIScrollView * mainScroll;//最底层滚动视图
//轮播图
@property (nonatomic,strong) SDCycleScrollView * headImageView;

@property (nonatomic,strong) UIView * twoButtonView;
//单品团购
@property (nonatomic,strong) UITableView * tableView1;
//品牌团购
@property (nonatomic,strong) UITableView * tableView2;
//新品团
@property (nonatomic,strong) UIButton * button1;
//品牌团
@property (nonatomic,strong) UIButton *button2;

@end

@implementation SXTTimeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"疯狂购物";
    self.edgesForExtendedLayout = 0;
    self.view.backgroundColor = MainColor;
    [self.view addSubview:self.mainScroll];
    [self.mainScroll addSubview:self.headImageView];
    [self.mainScroll addSubview:self.tableView1];
    [self.mainScroll addSubview:self.tableView2];
    [self.mainScroll addSubview:self.twoButtonView];
    [self.twoButtonView addSubview:self.button1];
    [self.twoButtonView addSubview:self.button2];
    [self requestHeadImage];
    
    // Do any additional setup after loading the view.
}

-(UIScrollView *)mainScroll{

    if (!_mainScroll) {
        _mainScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT)];
        _mainScroll.contentSize = CGSizeMake(0, 1980);
        _mainScroll.delegate = self;
    }
    return _mainScroll;
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if (scrollView.contentOffset.y > 230) {
        CGRect rect = _twoButtonView.frame;
        rect.origin.y = scrollView.contentOffset.y;
        _twoButtonView.frame = rect;
    }else{
    
        CGRect rect = _twoButtonView.frame;
        rect.origin.y = 230;
        _twoButtonView.frame = rect;
    }
}

-(SDCycleScrollView *)headImageView{

    if (!_headImageView) {
        _headImageView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 230) delegate:nil placeholderImage:[UIImage imageNamed:@"图标"]];
        _headImageView.backgroundColor = [UIColor orangeColor];
        _headImageView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _headImageView.currentPageDotColor = [UIColor whiteColor];
    }
    return _headImageView;
}

- (void)requestHeadImage{

    [self GETRequestURL:@"appHome/appHome.do"  param:nil isSuccess:^(id responseObject) {
        NSLog(@"responseObject : %@",responseObject);
        NSMutableArray * imageArray = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in responseObject) {
            NSString *imageUrl = dic[@"ImgView"];
            [imageArray addObject:imageUrl];
        }
        _headImageView.imageURLStringsGroup = imageArray;
    } isError:^(NSError *error) {
        NSLog(@"error : %@",error);
    }];
}

- (void)requestNew{


}

-(UIView *)twoButtonView{

    if (!_twoButtonView) {
        _twoButtonView = [[UIView alloc]initWithFrame:CGRectMake(0, 230, VIEW_WIDTH, 50)];
        _twoButtonView.backgroundColor = [UIColor whiteColor];
    }
    return _twoButtonView;
}

-(UIButton *)button1{

    if (!_button1) {
        _button1 = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _button1.frame = CGRectMake(0, 0, VIEW_WIDTH/2, 50);
        [_button1 addTarget:self action:@selector(changeTableFrame:) forControlEvents:(UIControlEventTouchUpInside)];
        [_button1 setTitle:@"新品团购" forState:(UIControlStateNormal)];
        [_button1 setImage:[UIImage imageNamed:@"限时特卖界面新品团购图标未选中"] forState:(UIControlStateNormal)];
        [_button1 setImage:[UIImage imageNamed:@"限时特卖界面新品团购图标"] forState:(UIControlStateSelected)];
        [_button1 setTitleColor:RGB(56, 166, 243) forState:(UIControlStateNormal)];
        [_button1 setTitleColor:RGB(213, 48, 34) forState:(UIControlStateSelected)];
        _button1.selected = YES;
    }
    return _button1;
}

-(UIButton *)button2{

    if (!_button2) {
        _button2 = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _button2.frame = CGRectMake(VIEW_WIDTH/2, 0, VIEW_WIDTH/2, 50);
        [_button2 addTarget:self action:@selector(changeTableFrame:) forControlEvents:(UIControlEventTouchUpInside)];
        [_button2 setTitle:@"品牌团购" forState:(UIControlStateNormal)];
        [_button2 setImage:[UIImage imageNamed:@"限时特卖界面品牌团购图标"] forState:(UIControlStateNormal)];
        [_button2 setImage:[UIImage imageNamed:@"限时特卖界面品牌团购图标选中"] forState:(UIControlStateSelected)];
        [_button2 setTitleColor:RGB(56, 166, 243) forState:(UIControlStateNormal)];
        [_button2 setTitleColor:RGB(213, 48, 34) forState:(UIControlStateSelected)];
        _button2.selected = NO;
    }
    return _button2;
}
-(void)changeTableFrame:(UIButton *)button{

    if (button == _button1) {
        button.selected = YES;
        _button2.selected = NO;
        [UIView animateWithDuration:1.0 animations:^{
            CGRect tableRect1 = _tableView1.frame;
            tableRect1.origin.x = 0;
            _tableView1.frame = tableRect1;
            
            CGRect tableRect2 = _tableView2.frame;
            tableRect2.origin.x = VIEW_WIDTH;
            _tableView2.frame = tableRect2;
        }];
    }else{
        button.selected = YES;
        _button1.selected = NO;
        [UIView animateWithDuration:1.0 animations:^{
            CGRect tableRect1 = _tableView1.frame;
            tableRect1.origin.x = -VIEW_WIDTH;
            _tableView1.frame = tableRect1;
            
            CGRect tableRect2 = _tableView2.frame;
            tableRect2.origin.x = 0;
            _tableView2.frame = tableRect2;
        }];
    }
}

-(UITableView *)tableView1{

    if (!_tableView1) {
        _tableView1 = [[UITableView alloc]initWithFrame:CGRectMake(0, 280, VIEW_WIDTH, 1700) style:(UITableViewStylePlain)];
        _tableView1.delegate = self;
        _tableView1.dataSource = self;
        _tableView1.bounces = NO;
    }
    return _tableView1;
}

-(UITableView *)tableView2{

    if (!_tableView2) {
        _tableView2 = [[UITableView alloc]initWithFrame:CGRectMake(VIEW_WIDTH, 280, VIEW_WIDTH, 1700) style:(UITableViewStylePlain)];
        _tableView2.delegate = self;
        _tableView2.dataSource = self;
        _tableView2.bounces = NO;
    }
    return _tableView2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 170;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"%li",indexPath.row];
    if (tableView == _tableView1) {
        cell.backgroundColor = [UIColor redColor];
    }else{
        cell.backgroundColor = [UIColor purpleColor];
    }
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
